﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdvUtils;

namespace WordSeg
{
    public class PinYinUtils
    {
        DictMatch dictmatch;
        List<Lemma> dm_r;
        List<int> dm_offsetList;

        public void Initialize(string strHZPYFileName)
        {
            dictmatch = new DictMatch();
            dm_r = new List<Lemma>();
            dm_offsetList = new List<int>();

            dictmatch.LoadDictFromRawText(strHZPYFileName);

        }

        public string ConvertTermsIntoPinYin(string strText)
        {
            //只使用字典进行切分
            dictmatch.Search(strText, ref dm_r, ref dm_offsetList, DictMatch.DM_OUT_FMM);
            int lastSegmentEndOffset = 0;
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < dm_r.Count; i++)
            {
                if (dm_offsetList[i] > lastSegmentEndOffset)
                {
                    //some lexical is not in dictionary
                    sb.Append(strText.Substring(lastSegmentEndOffset, dm_offsetList[i] - lastSegmentEndOffset));
                }

                int offset = dm_offsetList[i];
                int len = (int)dm_r[i].len;
                string strPY = dm_r[i].strProp;

                sb.Append(strPY);

                lastSegmentEndOffset = offset + len;
            }

            if (lastSegmentEndOffset < strText.Length)
            {
                //some lexical is not in dictionary
                sb.Append(strText.Substring(lastSegmentEndOffset));
            }

            return sb.ToString();
        }
    }
}
